package cn.tedu.project.oa.announcement.service.impl;

import cn.tedu.project.oa.announcement.mapper.AnnouncementMapper;
import cn.tedu.project.oa.announcement.pojo.dto.AnnouncementQuery;
import cn.tedu.project.oa.announcement.pojo.dto.AnnouncementSaveParam;
import cn.tedu.project.oa.announcement.pojo.entity.Announcement;
import cn.tedu.project.oa.announcement.pojo.vo.AnnouncementVO;
import cn.tedu.project.oa.announcement.service.AnnouncementService;
import cn.tedu.project.oa.base.response.PageData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class AnnouncementServiceImpl implements AnnouncementService {

    @Autowired
    AnnouncementMapper announcementMapper;

    @Override
    public PageData selectList(AnnouncementQuery announcementQuery) {
        log.debug("查询公告列表业务:announcementQuery={}",announcementQuery);

        AnnouncementQuery query = new AnnouncementQuery();
        Date startTime = announcementQuery.getStartTime();
        Date endTime = announcementQuery.getEndTime();
        BeanUtils.copyProperties(announcementQuery,query);
        List<AnnouncementVO> list = announcementMapper.selectList(query);

        PageData pageData = new PageData();
        pageData.setList(list);

        pageData.setTotal(announcementMapper.selectCount(announcementQuery));

        return pageData;
    }

    @Override
    public AnnouncementVO showAnnouncement(Long id) {
        log.debug("显示公告详情业务:id={}",id);

        AnnouncementVO announcementVO = announcementMapper.selectById(id);

        return announcementVO;

    }

    @Override
    public void saveAnnouncement(AnnouncementSaveParam announcementSaveParam) {
        log.debug("发布公告业务:announcementSaveParam={}",announcementSaveParam);

        Announcement announcement = new Announcement();
        BeanUtils.copyProperties(announcementSaveParam,announcement);
//        announcement.setUserId(20000l);
//        announcement.setDepartmentId(100l);
        announcement.setCreateTime(new Date());
        announcement.setStatus(10);
        announcementMapper.insert(announcement);

    }

    @Override
    public void deleteById(Long id) {
        log.debug("删除公告业务:id={}",id);

        announcementMapper.deleteById(id);
    }
}
